package com.hue.nio.day02.two;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/*
    客户端读取的线程
 */
public class ClientReadThread implements Runnable {
    private SocketChannel socketChannel;

    public ClientReadThread(SocketChannel socketChannel) {
        this.socketChannel = socketChannel;
    }

    @Override
    public void run() {
        while (true) {
            //获取socketChannel的信息打印
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            try {
                while (socketChannel.read(buffer) != 0) {
                    buffer.flip();
                    System.out.println(new String(buffer.array(), 0, buffer.limit()));
                    buffer.clear();
                }
            } catch (IOException e) {
            }
        }
    }
}
